<?php
	require_once("includes/checkCredentials.php");
	session_start(); 
	
	$login = "";
	$email = "";
	$disp_email = "";
	
	if ($_GET['lo'] == 1) { //LOG OUT
		$result = logOut();
		header('Location: index.php');
	}
	if ($_POST['email'] && $_POST['pword']) { //LOG IN
		$result = logIn($_POST['email'],$_POST['pword']);
		if ($result != 1) {
			$error = "Your email or password was incorrect. Please try again.";
			$email = $_POST['email'];
			$login = showLogIn($error,$email);
		} else {
			require_once("includes/PopulateLists.php");
			$lists = new PopulateLists;
			$disp_email = $_SESSION['email'];
		}
	} else if ($_POST['new_email'] && $_POST['new_pword']) { //SIGN UP
		$result = signUp($_POST['new_email'],$_POST['new_pword']);
		if ($result == -1) {
			$error = "Your email is already in use. Please try another one, or log in.";
			$email = $_POST['new_email'];
			$login = showLogIn($error,$email,1);
		} else {
			require_once("includes/PopulateLists.php");
			$lists = new PopulateLists;
			$disp_email = $_SESSION['email'];
		}
	} else {
		$newip = $_SERVER['REMOTE_ADDR']; 
		if (!isset($_SESSION['email']) || empty($_SESSION['email']) || $newip!= $_SESSION['ip']) { //NOT LOGGED IN
			$login = showLogIn();
		} else {
			require_once("includes/PopulateLists.php");
			$lists = new PopulateLists;
			$disp_email = $_SESSION['email'];
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Species Migration Model</title>
	<link rel="stylesheet" href="style.css" type="text/css" />
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAKU9Gv63dah1WqFl1797MlRSYVjwWdy7AbhNDg-RDrYETHew2-RSMwoJRrQ56Aj6TtmnHlKWs4v1HbQ" type="text/javascript"></script>
	<script src="js/tlabel.js" type="text/javascript"></script>
	<script src="js/map.js" type="text/javascript"></script>
  </head>
  <body onunload="GUnload()" onload="initialize()">
	<?php print $login ?>
	<div id="popupBG" class="hidden"></div>
	<div id="currentlyViewing" class="hidden" onchange="alert('hello');">5</div>
	<div id="content">
		<div id="loading"></div>
		<div id="top">
			<div class="right">
				<h6>Hello, <?php print $disp_email ?>!</h6>
				<h6>Not you? <a href="index.php?lo=1">Log out</a>.</h6>
			</div>
			<center><h2>Species Migration Model</h2></center>
		</div>
		<div id="l_sidebar">
			<div id="report_title"></div>
			<div id="report_text"></div>
		</div>
		<div id="map"></div>
		<div id="r_sidebar">
			<div id="actions">
				<h3>Actions</h3>
				<input type="button" name="nextYear" value="Next Year" onclick="var cv=getCurrentSpecies();var s=new Array(cv);call('functors/nextYearFunctor.php',s,1,1);">
				<input type="button" name="displaySpecies-showSpecies" value="Display Animal" onclick="showOption(this.name)">
				<input type="button" name="displaySpecies-compareSpecies-species2_title-options_dispSpecies2" value="Compare Animals" onclick="showOption(this.name)">
				<input type="button" name="addRemoveSpeciesToRegion-addSpecies" value="Add Animal" onclick="showOption(this.name)">
				<input type="button" name="addRemoveSpeciesToRegion-removeSpecies" value="Remove Animal" onclick="showOption(this.name)">
				<input type="button" name="addRemoveEnvEventToRegion-addEnvEventToRegion-options_durationTitle-options_duration" value="Add Event To A Region" onclick="showOption(this.name)">
				<input type="button" name="addRemoveEnvEventToRegion-removeEnvEventFromRegion" value="Remove Event From A Region" onclick="showOption(this.name)">
				<input type="button" name="addRemoveEnvEvent-addEnvEvent" value="Create Event" onclick="showOption(this.name)">
				<input type="button" name="removeEnvEvent" value="Delete Event" onclick="showOption(this.name)">
				<input type="button" name="restoreDefault" value="Restore Default Data" onclick="showOption(this.name);">
			</div>
			<div id="reports">
				<h3>Reports</h3>
				<input type="button" name="currentSpeciesInfo" value="Current Animal Info" onclick="setTitle('<h2>Animal Report</h2>');var cv=getCurrentSpecies(1);var s=new Array(cv);call('functors/animalReportFunctor.php',s,1,0);">
				<input type="button" name="allAnimalInfo" value="All Animal Info" onclick="setTitle('<h2>Animal Report</h2>');call('functors/allAnimalReportFunctor.php','',1,0);">
				<input type="button" name="plantInfo" value="Plant Info" onclick="showOption(this.name);">
				<input type="button" name="lastYear-lastYear_Animal-options_speciesLastYear-lastYearSubmit_Animal" value="Last Year By Animal" onclick="showOption(this.name);">
				<input type="button" name="lastYear-lastYear_Region-options_regionsLastYear-lastYearSubmit_Region" value="Last Year By Region" onclick="showOption(this.name);">
				
			</div>
		</div>
		<div id="bottom"></div>
	</div>
	
	<div id="lastYear" class="action hidden">
		<a href="#" class="close" onclick="hideOptions();">Close</a>
		<form name="lastYear" method="post" onsubmit="hideOptions();return false;">
			<h6 id="lastYear_Region" class="hidden">Select a Region</h6>
			<select class="hidden" id="options_regionsLastYear" name="options_regionsLastYear">
				<?php $lists->populateRegions(); ?>
			</select>
			<h6 id="lastYear_Animal" class="hidden">Select an Animal</h6>
			<select class="hidden" id="options_speciesLastYear" name="options_speciesLastYear">
				<?php $lists->populateSpecies(); ?>
			</select>
			<input class="hidden" id="lastYearSubmit_Region" type="submit" value="Show Report" onclick="setTitle('<h2>Last Year In '+$('options_regionsLastYear')[$('options_regionsLastYear').selectedIndex].innerHTML+'</h2>');var s=new Array('rid='+$('options_regionsLastYear')[$('options_regionsLastYear').selectedIndex].value+'');call('functors/yearlyReportFunctor.php',s,1,0);" />
			<input class="hidden" id="lastYearSubmit_Animal" type="submit" value="Show Report" onclick="setTitle('<h2>Last Year For '+$('options_speciesLastYear')[$('options_speciesLastYear').selectedIndex].innerHTML+'</h2>');var s=new Array('sid='+$('options_speciesLastYear')[$('options_speciesLastYear').selectedIndex].value+'');call('functors/animalYearlyReportFunctor.php',s,1,0);" />
		</form>
	</div>
	
	<div id="plantInfo" class="action hidden">
		<a href="#" class="close" onclick="hideOptions();">Close</a>
		<form name="plantInfo" method="post" onsubmit="hideOptions();return false;">
			<h6>Select a Plant</h6>
			<select id="options_plantInfo" name="options_plantInfo">
				<?php $lists->populatePlants(); ?>
			</select>
			<input type="submit" value="Show Info" onclick="setTitle('<h2>Plant Report</h2>');var s=new Array('sid='+$('options_plantInfo')[$('options_plantInfo').selectedIndex].value+'');call('functors/plantReportFunctor.php',s,1,0);" />
		</form>
	</div>
	
	<div id="addRemoveSpeciesToRegion" class="action hidden">
		<a href="#" class="close" onclick="hideOptions();">Close</a>
		<form name="addRemoveSpeciesToRegion" method="post" onsubmit="hideOptions();return false;">
			<h6>Add/Remove Species</h6>
			<select id="options_species" name="options_species">
				<?php $lists->populateSpecies(); ?>
			</select>
			<h6>To/From Region</h6>
			<select id="options_regions" name="options_regions">
				<?php $lists->populateRegions(); ?>
			</select>
			<h6>How Many?</h6>
			<input id="options_population" type="text" name="options_population" value="0" size="5" />
			<input id="addSpecies" class="hidden" type="submit" value="Add Species" onclick="var s=new Array('selectSpecies='+$('options_species')[$('options_species').selectedIndex].value+'','selectRegion='+$('options_regions')[$('options_regions').selectedIndex].value+'','population='+$('options_population').value+'');call('functors/addSpeciesToRegionFunctor.php',s,0,1);" />
			<input id="removeSpecies" class="hidden" type="submit" value="Remove Species" onclick="var s=new Array('selectSpecies='+$('options_species')[$('options_species').selectedIndex].value+'','selectRegion='+$('options_regions')[$('options_regions').selectedIndex].value+'','population='+$('options_population').value+'');call('functors/removeSpeciesFromRegionFunctor.php',s,0,1);" />			
		</form>
	</div>
	
	<div id="addRemoveEnvEventToRegion" class="action hidden">
		<a href="#" class="close" onclick="hideOptions();">Close</a>
		<form name="addRemoveEnvEventToRegion" method="post" onsubmit="hideOptions();return false;">
			<h6>Add/Remove Event</h6>
			<select id="options_envevent" name="envEventsSelect">
				<?php $lists->populateEnvironmentEvents(); ?>
			</select>
			<h6>To/From Region</h6>
			<select id="options_regions2" name="selectRegion">
				<?php $lists->populateRegions(); ?>
			</select>
			<h6 id="options_durationTitle" class="hidden">How Long? (yrs)</h6>
			<input id="options_duration" class="hidden" type="text" name="options_duration" value="1" size="5" />
			<input id="addEnvEventToRegion" class="hidden" type="submit" value="Add Event" onclick="var s=new Array('selectEnvironment='+$('options_envevent')[$('options_envevent').selectedIndex].value+'','selectRegion='+$('options_regions2')[$('options_regions2').selectedIndex].value+'','duration='+$('options_duration').value+'');call('functors/addEventToRegionFunctor.php',s,0,0);" />
			<input id="removeEnvEventFromRegion" class="hidden" type="submit" value="Remove Event" onclick="var s=new Array('selectEnvironment='+$('options_envevent')[$('options_envevent').selectedIndex].value+'','selectRegion='+$('options_regions2')[$('options_regions2').selectedIndex].value+'');call('functors/removeEventFromRegionFunctor.php',s,0,0);" />
		</form>
	</div>
	
	<div id="addRemoveEnvEvent" class="action hidden">
		<a href="#" class="close" onclick="hideOptions();">Close</a>
		<form name="addRemoveEnvEvent" method="post" onsubmit="hideOptions();return false;">
			<h6>Event Name</h6>
			<input id="options_envEventName" type="text" name="options_envEventName" value="" size="25" />
			<h6>Annual Rainfall Change (+/-) (mm/yr)</h6>
			<input id="options_envEventPrec" type="text" name="options_envEventPrec" value="0" size="4" />
			<h6>Average Temperature Change (+/-) (&deg; F)</h6>
			<input id="options_envEventHeat" type="text" name="options_envEventHeat" value="0" size="4" />
			<h6>Average Sunlight (+/-) (hrs/day)</h6>
			<input id="options_envEventSun" type="text" name="options_envEventSun" value="0" size="4" />
			<input id="addEnvEvent" class="hidden" type="submit" value="Add Event" onclick="var s=new Array('eventName='+$('options_envEventName').value+'','eventPrec='+$('options_envEventPrec').value+'','eventHeat='+$('options_envEventHeat').value+'','eventSun='+$('options_envEventSun').value+'');call('functors/addEnvironmentEventFunctor.php',s);refreshEvents();" />
		</form>
	</div>
	
	<div id="removeEnvEvent" class="action hidden">
		<a href="#" class="close" onclick="hideOptions();">Close</a>
		<form name="removeEnvEvent" method="post" onsubmit="hideOptions();return false;">
			<h6>Remove Event</h6>
			<select id="options_envevent2" name="options_envevent2">
				<?php $lists->populateEnvironmentEvents(); ?>
			</select>
			<input type="submit" value="Remove Event" onclick="var s=new Array('removeID='+$('options_envevent2')[$('options_envevent2').selectedIndex].value+'');call('functors/removeEnvironmentEventFunctor.php',s);refreshEvents();" />
		</form>
	</div>
	
	<div id="displaySpecies" class="action hidden">
		<a href="#" class="close" onclick="hideOptions();">Close</a>
		<form name="displaySpeciesForm" method="post" onsubmit="hideOptions();return false;">
			<h6>Select a Species</h6>
			<select id="options_dispSpecies" name="options_dispSpecies">
				<?php $lists->populateSpecies(); ?>
			</select>
			<h6 id="species2_title" class="hidden">Select another Species</h6>
			<select id="options_dispSpecies2" name="options_dispSpecies2" class="hidden">
				<?php $lists->populateSpecies(); ?>
			</select>
			<input id="showSpecies" class="hidden" type="submit" value="Show Species" onclick="var s=new Array('selectSpecies='+$('options_dispSpecies')[$('options_dispSpecies').selectedIndex].value+'');call('functors/displayPopulationFunctor.php',s,0,0);" />
			<input id="compareSpecies" class="hidden" type="submit" value="Compare Species" onclick="var s=new Array('firstSpecies='+$('options_dispSpecies')[$('options_dispSpecies').selectedIndex].value+'','secondSpecies='+$('options_dispSpecies2')[$('options_dispSpecies2').selectedIndex].value+'');call('functors/compareSpeciesFunctor.php',s,0,0);" />
		</form>
	</div>
	
	<div id="restoreDefault" class="action hidden">
		<a href="#" class="close" onclick="hideOptions();">Close</a>
		<form name="restoreDefaultForm" method="post" onsubmit="hideOptions();return false;">
			<h5>Are You Sure?</h5>
			<p>All of your data will be restored to the default data. All changes you have made will be lost. This cannot be undone.</p>
			<input type="submit" value="No" />
			<input type="submit" value="OK" onclick="call('functors/restoreDefaultFunctor.php','',0,0);" />
		</form>
	</div>
  </body>
</html>